import Vue from 'vue'
import Router from 'vue-router'
import AppIndex from "../components/index/AppIndex";
import Shop from "../components/shop/Shop";
import Add from "../components/shop/Add";
import ShopImg from "../components/shop/ShopImg";
import Order from "../components/Order/Order";
import AddImg from "../components/shop/AddImg";
import { Message } from "element-ui";

Vue.use(Router)

let router= new Router({

  routes: [
    {path: '/', redirect:'/index'},
    {path: '/index', name: 'AppIndex', component: AppIndex},
    {path: '/shop', name: 'Shop', component: Shop},
    {path: '/add', name: 'Add', component: Add,
      children:[{path: 'addImg', name: 'AddImg', component: AddImg},]
    },
    {path: '/shopImg', name: 'ShopImg', component: ShopImg},
    {path: '/order', name: 'Order', component: Order,},

  ]
})
// 导航守卫

// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.path === '/index') {
    next();
  } else {
    let token = sessionStorage.getItem('token');
    if (token === null || token === '') {
     Message.warning("请先登录！")
      next('/index');
    } else {
      next();
    }
  }
})
export default router

